using Moq;
using NUnit.Framework;
using RMH.DataAccess;
using RMH.Entities;
using RMH.Services;

namespace RMH.UnitTests.Services
{
    [TestFixture]
    public class PatientService_when_saving_a_patient_with_relationsihps : base_automock_test
    {
        private Patient patient = new Patient {FirstName = "John"};

        public override void establish_context()
        {
            patient.Relationships.Add(new Relationship { Patient = patient });
            patient.Relationships.Add(new Relationship { Id = 2, Patient = patient });
        }

        public override void because()
        {
            Create<PatientService>().Save(patient);
        }

        [Test]
        public void relationshipRepository_saves_all_unsaved_relationships()
        {
            Mock<IRelationshipRepository>()
                .Verify(s=>s.Save(It.Is<Relationship>(r=>r.Patient == patient)),
                Times.Exactly(1));
        }

        [Test]
        public void repository_saves_patient()
        {
            Mock<IPatientRepository>()
             .Verify(s => s.Save(It.Is<Patient>(r => r.Id== patient.Id)),
             Times.Exactly(1));
        }
    }


    [TestFixture]
    public class BaseServices_when_saving_a_patient : base_automock_test
    {
        private Patient patientToSave = new Patient {Id = 1};
        public override void because()
        {
            Create<PatientService>().Save(patientToSave);
        }

        [Test]
        public void the_patient_repository_saves_the_patient()
        {
            Mock<IPatientRepository>()
                .Verify(s=>s.Save(patientToSave));
        }        
    }
}